import numpy

from . import prediction_bp
from . import Prediction
from flask import jsonify
from flask import request
import json
import pandas as pd
import numpy as np


@prediction_bp.route('/generate_model/')
def generate_model():
    Prediction.generate_model()
    return jsonify({'msg': '生成成功'})


@prediction_bp.route('/generate_price/', methods=['POST'])
def generate_price():
    pre_dic = json.loads(request.get_data(as_text=True))

    f = pre_dic.pop('property_floor')
    o = pre_dic.pop('property_orientation')
    l = pre_dic.pop('property_loc')
    f_key = f'property_floor_{f}'
    o_key = f'property_orientation_{o}'
    l_key = f'property_loc_{l}'
    pre_dic[f_key] = 1
    pre_dic[o_key] = 1
    pre_dic[l_key] = 1
    df = pd.read_csv('./data/SJZ_Property.csv')
    df = df.drop(labels=['Unnamed: 0', 'property_price'], axis=1)
    col_list = df.columns.tolist()
    pre_list = []
    for item in col_list:
        if item in pre_dic.keys():

            pre_list.append(pre_dic[item])
        else:
            pre_list.append(0)
    # result = Prediction.predict(numpy.array(pre_list))
    result = Prediction.predict([pre_list])
    return str(result[0])
